// pages/index/letters_visits/letters_visits.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 4, //预约类型 （1.社保; 2.综; 3.视频探视预约; 4.信访预约(我帮你办事)）

    //我的预约
    isRefresh: false,
    isLoading: false,
    reservePageNumber: 1,
    reserveList: [],
    //信访宣传
    isPublicityRefresh: false,
    isPublicityLoading: false,
    publicityList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onRefresh()
    this.onPublicityRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 我的预约
   */
  onRefresh: function (e) {
    if (!this.data.isRefresh) {
      this.setData({
        reservePageNumber: 1,
        reserveList: [],
        isRefresh: true
      })
      this.getReserveList()
    }
  },
  onLower: function (e) {
    this.data.reservePageNumber++
    this.getReserveList()
  },
  /**
   * 查询我的预约
   */
  getReserveList: function () {
    this.setData({
      isLoading: true
    })
    http.get(app.globalData.xw_api_url + "online/order/page",{
      page_number: this.data.reservePageNumber,
      page_size: 10,
      area_code: app.globalData.areaCode,
      applet_user_id: app.globalData.userInfo.applet_user_id,
      type: this.data.type
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        if (res.data.data.list.length > 0) {
          let list = this.data.reserveList
          res.data.data.list.forEach(item => {
            list.push(item)
          })
          this.setData({
            reserveList: list
          })
        } else {
          if (this.data.reservePageNumber > 1) {
            this.data.reservePageNumber--
            Toast.fail("没有更多数据了")
          }
        }
      } else {
        if (this.data.reservePageNumber > 1) {
          this.data.reservePageNumber--
        }
        Toast.fail(res.data.msg)
      }
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    },err => {
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    })
  },

  /**
   * 预约申请
   * @param e
   */
  onAddReserve: function (e) {
    wx.navigateTo({
      url: "./add/add"
    })
  },


  /**
   * 信访宣传
   */

  /**
   * 信访宣传 -- 下拉刷新
   * @param e
   */
  onPublicityRefresh: function (e) {
    this.setData({
      isPublicityRefresh: true,
      publicityList: []
    })
    this.getPublicityList()
  },

  /**
   * 获取信访宣传列表
   */
  getPublicityList: function () {
    this.setData({
      isPublicityLoading: true
    })
    http.get(app.globalData.xw_api_url + "notice/open/list",{
      type: 19,
      applet_id: app.globalData.applet_id,
      unit_code: app.globalData.unitCode,
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        this.setData({
          publicityList: res.data.data
        })
      } else {
        Toast.fail(res.data.msg)
      }
      this.setData({
        isPublicityLoading: false,
        isPublicityRefresh: false
      })
    }, err => {
      this.setData({
        isPublicityLoading: false,
        isPublicityRefresh: false
      })
      Toast.fail("网络异常，请稍后再试")
    })
  },

  /**
   * 跳转详情
   * @param e
   */
  onDetails: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "../article/details/details?id=" + id
    })
  },

})